package huoye.shops.base;

import com.codeideology.android.lang.entity.Paging;
import com.codeideology.android.log.LogUtils;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;

public class MineJsonResponseHandler {
    protected Gson gson;
    private boolean isRecord;
    GsonBuilder builder = new GsonBuilder();
    private int status;
    private boolean success ;
    String exception;
    String description;
    private Paging paging;

    public MineJsonResponseHandler() {
    }

    public MineJsonResponseHandler(boolean isRecord) {
        this.isRecord = isRecord;
    }

    protected String getSubNodeStringValue(JsonObject jsonObject, String nodeName) {
        JsonElement subNode = jsonObject.get(nodeName);
        return subNode != null?subNode.getAsString():null;
    }

    protected boolean getSubNodeBooleanValue(JsonObject jsonObject, String nodeName) {
        JsonElement subNode = jsonObject.get(nodeName);
        return subNode.getAsBoolean();
    }

    protected <P> P jsonToBean(JsonObject jsonObject, Class<P> clazz) {
        return this.gson.fromJson(jsonObject, clazz);
    }

    public GsonBuilder getGsonBuilder() {
        return this.builder;
    }

    public void parse(String json) throws Exception {
      //  this.success = true;
        this.gson = this.builder.setFieldNamingStrategy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
        JsonObject root = (JsonObject)this.gson.fromJson(json, JsonObject.class);
        this.parseStatus(root);
        LogUtils.d("parse", new Object[]{"status"});
        this.parseRemoteBusinessException(root);
        LogUtils.d("parse", new Object[]{"business"});
        this.parseExtra(root);

        try {
            if(root.has("data")) {
                JsonElement recordNodeElement = root.get("data");
                if(recordNodeElement.isJsonArray()) {
                    JsonArray recordsetNodes = recordNodeElement.getAsJsonArray();
                    this.parseRecordset(recordsetNodes);
                    LogUtils.d("parse", new Object[]{"list"});
                } else if(recordNodeElement.isJsonObject()) {
                    LogUtils.d("parse", new Object[]{"object"});
                    JsonObject recordNode = recordNodeElement.getAsJsonObject();
                    if(recordNode != null) {
                        if(recordNode.has("items") && this.isRecord) {
                            this.parseRecord(recordNode);
                            LogUtils.d("parse", new Object[]{"parseRecord1"});
                            this.parsePaging(recordNode);
                            LogUtils.d("parse", new Object[]{"parsePaging"});
                        } else if(recordNode.has("items")) {
                            if(recordNode.get("items").isJsonArray()) {
                                JsonArray recordsetNodes = recordNode.getAsJsonArray("items");
                                if(recordsetNodes != null) {
                                    this.parseRecordset(recordsetNodes);
                                    LogUtils.d("parse", new Object[]{"parseRecordset"});
                                }
                            }

                            this.parsePaging(recordNode);
                            LogUtils.d("parse", new Object[]{"parsePaging1"});
                        } else if(recordNode.has("data")) {
                            if(recordNode.get("data").isJsonObject()) {
                                JsonObject jsonNode = recordNode.getAsJsonObject("data");
                                if(jsonNode != null) {
                                    this.parseRecord(jsonNode);
                                    LogUtils.d("parse", new Object[]{"jsonNode"});
                                }
                            }
                        } else {
                            this.parseRecord(recordNode);
                            LogUtils.d("parse", new Object[]{"parseRecord3"});
                        }
                    }
                } else {
                    this.parseRecord(root);
                    LogUtils.d("parse", new Object[]{"parseRecord root"});
                }

            }
        } catch (JsonSyntaxException var6) {
            throw new Exception(var6);
        }
    }

    public int getStatus() {
        return this.status;
    }

    private void parseStatus(JsonObject root) {



        if(root.has("code")) {
            JsonElement jsonElement = root.get("code");
            if(jsonElement.isJsonPrimitive()) {
                this.status = jsonElement.getAsInt();
            }
        }

        this.success =  this.getSubNodeBooleanValue(root, "success");

        if(this.status != 1000) {
            this.description = this.getSubNodeStringValue(root, "msg");
            // this.success = false;
        }

    }

    public boolean isSuccess() {
        return this.success;
    }

    private void parseRemoteBusinessException(JsonObject root) {
        if(root.has("exception")) {
            JsonElement exceptionNode = root.get("exception");
            if(exceptionNode.isJsonPrimitive()) {
                this.exception = exceptionNode.getAsString();
            }
        }

        this.description = this.getSubNodeStringValue(root, "msg");
    }

    public String getException() {
        return this.exception;
    }

    public String getDescription() {
        return this.description;
    }

    protected void parseExtra(JsonObject root) {
    }

    protected void parseRecord(JsonObject recordNode) {
    }

    protected void parseRecordset(JsonArray recordsetNodes) {
    }

    public Paging getPaging() {
        return this.paging;
    }

    protected void parsePaging(JsonObject root) {
        this.paging = new Paging();
        if(root.has("count") && root.get("count").isJsonPrimitive()) {
            this.paging.setRecords(root.get("count").getAsInt());
        }

    }
}
